/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package GUI;

import engine.linkList.LinkedListManager;
import java.awt.CardLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author hjp
 */
public class createMusicListDialog extends javax.swing.JDialog {

    /**
     * Creates new form createMusicList
     */
    public createMusicListDialog(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        setUndecorated(true);
        initComponents();
        this.addWindowListener(new java.awt.event.WindowAdapter() {
                 @Override
                  public void windowClosing(java.awt.event.WindowEvent e) {
                      System.exit(0);
                  }
       });
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jPanel2 = new javax.swing.JPanel();
        createNewMusicListButton = new javax.swing.JButton();
        cancleButton = new javax.swing.JButton();
        jPanel3 = new javax.swing.JPanel();
        newMusicListTitleTextFiled = new javax.swing.JTextField();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setBackground(new java.awt.Color(250, 250, 250));
        getContentPane().setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        jPanel1.setBackground(new java.awt.Color(250, 250, 250));

        jLabel1.setFont(new java.awt.Font("楷体", 0, 24)); // NOI18N
        jLabel1.setText("新建歌单");
        jLabel1.setBorder(javax.swing.BorderFactory.createEmptyBorder(1, 1, 1, 1));
        jLabel1.setCursor(new java.awt.Cursor(java.awt.Cursor.MOVE_CURSOR));
        jLabel1.addMouseMotionListener(new java.awt.event.MouseMotionAdapter() {
            public void mouseDragged(java.awt.event.MouseEvent evt) {
                jLabel1MouseDragged(evt);
            }
        });
        jLabel1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mousePressed(java.awt.event.MouseEvent evt) {
                jLabel1MousePressed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, 335, Short.MAX_VALUE)
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, 77, Short.MAX_VALUE)
        );

        getContentPane().add(jPanel1, new org.netbeans.lib.awtextra.AbsoluteConstraints(0, 0, -1, -1));

        jPanel2.setBackground(new java.awt.Color(250, 250, 250));

        createNewMusicListButton.setBackground(new java.awt.Color(250, 250, 250));
        createNewMusicListButton.setFont(new java.awt.Font("楷体", 0, 24)); // NOI18N
        createNewMusicListButton.setText("创建");
        createNewMusicListButton.setCursor(new java.awt.Cursor(java.awt.Cursor.HAND_CURSOR));
        createNewMusicListButton.setFocusPainted(false);
        createNewMusicListButton.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                createNewMusicListButtonMouseClicked(evt);
            }
        });

        cancleButton.setBackground(new java.awt.Color(250, 250, 250));
        cancleButton.setFont(new java.awt.Font("楷体", 0, 24)); // NOI18N
        cancleButton.setText("取消");
        cancleButton.setCursor(new java.awt.Cursor(java.awt.Cursor.HAND_CURSOR));
        cancleButton.setFocusPainted(false);
        cancleButton.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                cancleButtonMouseClicked(evt);
            }
        });

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addComponent(createNewMusicListButton, javax.swing.GroupLayout.PREFERRED_SIZE, 149, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(cancleButton, javax.swing.GroupLayout.PREFERRED_SIZE, 182, javax.swing.GroupLayout.PREFERRED_SIZE))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(createNewMusicListButton, javax.swing.GroupLayout.DEFAULT_SIZE, 60, Short.MAX_VALUE)
            .addComponent(cancleButton, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );

        getContentPane().add(jPanel2, new org.netbeans.lib.awtextra.AbsoluteConstraints(0, 190, 330, 60));

        jPanel3.setBackground(new java.awt.Color(250, 250, 250));
        jPanel3.setBorder(new javax.swing.border.LineBorder(new java.awt.Color(0, 0, 0), 1, true));

        newMusicListTitleTextFiled.setBackground(new java.awt.Color(250, 250, 250));
        newMusicListTitleTextFiled.setFont(new java.awt.Font("楷体", 0, 24)); // NOI18N
        newMusicListTitleTextFiled.setText("请输入新歌单标题");
        newMusicListTitleTextFiled.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusGained(java.awt.event.FocusEvent evt) {
                newMusicListTitleTextFiledFocusGained(evt);
            }
            public void focusLost(java.awt.event.FocusEvent evt) {
                newMusicListTitleTextFiledFocusLost(evt);
            }
        });
        newMusicListTitleTextFiled.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                newMusicListTitleTextFiledMouseClicked(evt);
            }
        });
        newMusicListTitleTextFiled.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                newMusicListTitleTextFiledActionPerformed(evt);
            }
        });
        newMusicListTitleTextFiled.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                newMusicListTitleTextFiledKeyTyped(evt);
            }
        });

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(newMusicListTitleTextFiled, javax.swing.GroupLayout.DEFAULT_SIZE, 298, Short.MAX_VALUE)
                .addContainerGap())
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addGap(30, 30, 30)
                .addComponent(newMusicListTitleTextFiled, javax.swing.GroupLayout.PREFERRED_SIZE, 41, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(37, Short.MAX_VALUE))
        );

        getContentPane().add(jPanel3, new org.netbeans.lib.awtextra.AbsoluteConstraints(0, 80, 330, 110));

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void newMusicListTitleTextFiledActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_newMusicListTitleTextFiledActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_newMusicListTitleTextFiledActionPerformed

    private void jLabel1MousePressed(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jLabel1MousePressed
        // TODO add your handling code here:
        locationX = evt.getX();
        locationY = evt.getY();
    }//GEN-LAST:event_jLabel1MousePressed

    private void jLabel1MouseDragged(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jLabel1MouseDragged
        // TODO add your handling code here:
        int left = this.getLocation().x;
        int top = this.getLocation().y;
        this.setLocation(left + evt.getX() - locationX, top + evt.getY() - locationY);
    }//GEN-LAST:event_jLabel1MouseDragged

    private void cancleButtonMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_cancleButtonMouseClicked
        // TODO add your handling code here:
        dispose();
    }//GEN-LAST:event_cancleButtonMouseClicked

    private void newMusicListTitleTextFiledMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_newMusicListTitleTextFiledMouseClicked
        //首次点击文本框的时候清空文本框
        if (cnt == 1) {
            newMusicListTitleTextFiled.setText("");
            ++cnt;
        }
    }//GEN-LAST:event_newMusicListTitleTextFiledMouseClicked

    private void createNewMusicListButtonMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_createNewMusicListButtonMouseClicked
            char []ch = newMusicListTitleTextFiled.getText().toCharArray();
            boolean flag = true;//用于检测字符串中是否仅包括空格
            for(char c : ch){
                if (c != ' ') {
                    flag = false;
                    break;
                }
            }
            if (!flag) {
                JButton newButton = new JButton(newMusicListTitleTextFiled.getText().trim() + "            ");
                newButton.setCursor(new java.awt.Cursor(java.awt.Cursor.HAND_CURSOR));
                newButton.setFont((new java.awt.Font("楷体", 0, 32)));
                newButton.addMouseListener(new MouseAdapter(){
                        @Override
                        public void mouseClicked(MouseEvent e) {
                            //屏蔽选择目录以及匹配音乐按钮
                            MusicPlayerUI.getInstance().getAddLocalToScanFolderNodeButton().setVisible(false);
                            MusicPlayerUI.getInstance().getMatchMusicButton().setVisible(false);
                            //设置currentVisitMusicListLabel
                            MusicPlayerUI.getInstance().getCurrentVisitMusicListLabel().setText(newMusicListTitleTextFiled.getText().trim());
                            //修改当前访问的链表集合
                            LinkedListManager.getInstance().setCurrentVisitMusicLists(LinkedListManager.getInstance().getCreateMusicLists());
                            //修改当前访问的链表
                            LinkedListManager.getInstance().setCurrentVisitMusicList(LinkedListManager.getInstance().findSetectedMusicList(LinkedListManager.getInstance().getCreateMusicLists(), newMusicListTitleTextFiled.getText().trim()));
                            //为中间变量赋值
                            MusicPlayerUI.getInstance().transientCurrentVisitMusicLists = LinkedListManager.getInstance().getCurrentVisitMusicLists();
                            MusicPlayerUI.getInstance().transientCurrentVisitMusicList = LinkedListManager.getInstance().getCurrentVisitMusicList();
                            //将该链表的内容送入musicListTabel中
                            MusicPlayerUI.getInstance().printMusicList(MusicPlayerUI.getInstance().getMusicListTabel());
                            //如果CardLayout当前显示的是card2，则强行返回card1
                            CardLayout card = (CardLayout)MusicPlayerUI.getInstance().getListPanel().getLayout();
                            card.show(MusicPlayerUI.getInstance().getListPanel(), "card1");
                            //置当前访问card指针为card1
                            MusicPlayerUI.getInstance().currentCard = "card1";
                        }
                });
                newButton.setBackground(new Color(250,250,250));
                JPanel panel  = MusicPlayerUI.getInstance().getCreateMusicListsPanel();
                panel.add(newButton);
                panel.revalidate();
                //将创建的链表添加到链表管理器中
                LinkedListManager.getInstance().createNewMusicList(newMusicListTitleTextFiled.getText());
           }
           dispose();
    }//GEN-LAST:event_createNewMusicListButtonMouseClicked

    private void newMusicListTitleTextFiledKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_newMusicListTitleTextFiledKeyTyped
        // TODO add your handling code here:
    }//GEN-LAST:event_newMusicListTitleTextFiledKeyTyped

    private void newMusicListTitleTextFiledFocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_newMusicListTitleTextFiledFocusGained
        // TODO add your handling code here:
    }//GEN-LAST:event_newMusicListTitleTextFiledFocusGained

    private void newMusicListTitleTextFiledFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_newMusicListTitleTextFiledFocusLost
        // TODO add your handling code here:
    }//GEN-LAST:event_newMusicListTitleTextFiledFocusLost

    public String getMusicListTitle(){
        return newMusicListTitleTextFiled.getText();
    }
    
    private int locationX;
    private int locationY;
    private int cnt = 1;
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton cancleButton;
    private javax.swing.JButton createNewMusicListButton;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JTextField newMusicListTitleTextFiled;
    // End of variables declaration//GEN-END:variables
}
